<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>logbuffer_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_debug_logbuffer_test.js.html">logbuffer_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.debug.LogBufferTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.debug.LogBufferTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.debug.LogBuffer&#39;);
<a name="line19"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line20"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line21"></a>
<a name="line22"></a>var DUMMY_LEVELS = [
<a name="line23"></a>  goog.debug.Logger.Level.INFO,
<a name="line24"></a>  goog.debug.Logger.Level.WARNING,
<a name="line25"></a>  goog.debug.Logger.Level.SEVERE];
<a name="line26"></a>var DUMMY_MESSAGES = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;];
<a name="line27"></a>var DUMMY_NAMES = [&#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;];
<a name="line28"></a>
<a name="line29"></a>var buffer;
<a name="line30"></a>var dummyIndex = 0;
<a name="line31"></a>
<a name="line32"></a>function setUp() {
<a name="line33"></a>  goog.debug.LogBuffer.CAPACITY = 4;
<a name="line34"></a>  goog.debug.LogBuffer.instance_ = null;
<a name="line35"></a>  buffer = goog.debug.LogBuffer.getInstance();
<a name="line36"></a>}
<a name="line37"></a>
<a name="line38"></a>function verifyRecord(expectedIndex, record) {
<a name="line39"></a>  var index = expectedIndex % DUMMY_MESSAGES.length;
<a name="line40"></a>  var message = DUMMY_MESSAGES[index];
<a name="line41"></a>  var level = DUMMY_LEVELS[index];
<a name="line42"></a>  var name = DUMMY_NAMES[index];
<a name="line43"></a>  assertEquals(&#39;Wrong level for record &#39; + expectedIndex, level,
<a name="line44"></a>      record.getLevel());
<a name="line45"></a>  assertEquals(&#39;Wrong message for record &#39; + expectedIndex, message,
<a name="line46"></a>      record.getMessage());
<a name="line47"></a>  assertEquals(&#39;Wrong name for record &#39; + expectedIndex, name,
<a name="line48"></a>      record.getLoggerName());
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function addAndVerifyRecord() {
<a name="line52"></a>  var index = dummyIndex % DUMMY_MESSAGES.length;
<a name="line53"></a>  var level = DUMMY_LEVELS[index];
<a name="line54"></a>  var message = DUMMY_MESSAGES[index];
<a name="line55"></a>  var name = DUMMY_NAMES[index];
<a name="line56"></a>  var record = buffer.addRecord(level, message, name);
<a name="line57"></a>  verifyRecord(dummyIndex, record);
<a name="line58"></a>  dummyIndex++;
<a name="line59"></a>}
<a name="line60"></a>
<a name="line61"></a>function addSomeRecords(howMany) {
<a name="line62"></a>  for (var i = 0; i &lt; howMany; i++) {
<a name="line63"></a>    addAndVerifyRecord();
<a name="line64"></a>  }
<a name="line65"></a>}
<a name="line66"></a>
<a name="line67"></a>function testAddRecord() {
<a name="line68"></a>  addSomeRecords(goog.debug.LogBuffer.CAPACITY * 3);
<a name="line69"></a>}
<a name="line70"></a>
<a name="line71"></a>function testIsFull() {
<a name="line72"></a>  assertFalse(&#39;Should not be full.&#39;, buffer.isFull_);
<a name="line73"></a>  addSomeRecords(goog.debug.LogBuffer.CAPACITY * 1.5);
<a name="line74"></a>  assertTrue(&#39;Should be full.&#39;, buffer.isFull_);
<a name="line75"></a>  buffer.clear();
<a name="line76"></a>  assertFalse(&#39;Should not be full after clear().&#39;, buffer.isFull_);
<a name="line77"></a>  addSomeRecords(goog.debug.LogBuffer.CAPACITY - 1);
<a name="line78"></a>  assertFalse(&#39;Should not be full but almost full.&#39;, buffer.isFull_);
<a name="line79"></a>}
<a name="line80"></a>
<a name="line81"></a>function testForEachRecord() {
<a name="line82"></a>  // Test with it half full.
<a name="line83"></a>  var howMany1 = goog.debug.LogBuffer.CAPACITY / 2;
<a name="line84"></a>  addSomeRecords(howMany1);
<a name="line85"></a>  var counter1 = 0;
<a name="line86"></a>  buffer.forEachRecord(function(record) {
<a name="line87"></a>    verifyRecord(counter1++, record);
<a name="line88"></a>  });
<a name="line89"></a>  assertEquals(&#39;Wrong number of records when half full.&#39;, howMany1, counter1);
<a name="line90"></a>
<a name="line91"></a>  // Test with it full.
<a name="line92"></a>  var howMany2 = goog.debug.LogBuffer.CAPACITY;
<a name="line93"></a>  addSomeRecords(howMany2);
<a name="line94"></a>  var index = counter1;
<a name="line95"></a>  buffer.forEachRecord(function(record) {
<a name="line96"></a>    verifyRecord(index++, record);
<a name="line97"></a>  });
<a name="line98"></a>  assertEquals(&#39;Wrong number of records when full.&#39;, howMany1 + howMany2,
<a name="line99"></a>      index);
<a name="line100"></a>}
</pre>


</body>
</html>
